package com.platform.hjt.erp.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hjt.tools.dao.Page;
import com.hjt.tools.dao.PropertyFilter;
import com.platform.hjt.base.controller.BaseController;
import com.platform.hjt.base.controller.ReturnBody;
import com.platform.hjt.erp.model.ErpGoodInfo;
import com.platform.hjt.erp.service.ErpGoodInfoService;
import com.platform.hjt.sys.service.SysStateService;

@Controller
@RequestMapping(value="/erpGoodInfo")
public class ErpGoodInfoController extends BaseController{

	@Resource
	private ErpGoodInfoService erpGoodInfoService;
	@Resource
	private SysStateService sysStateService;
	
	@RequestMapping(value="/erpGoodInfo_list.html")
	public String erpGoodInfoListPage(){
		return "page/admin/erpGoodInfo/erpGoodInfo_list";
	}
	
	@RequestMapping(value="/erpGoodInfo_list.do")
	@ResponseBody
	public Page<ErpGoodInfo> erpGoodInfoListDo(Page<ErpGoodInfo>page){
		List<PropertyFilter>  propertyFilterList = PropertyFilter.buildFromHttpRequest(getRequest());
		propertyFilterList.add(new PropertyFilter("EQB_isDel", "false"));
		propertyFilterList.add(new PropertyFilter("EQS_sysCompanyInfo.sysCompanyInfoId", getSessionSysCompanyInfo().getSysCompanyInfoId()));
		return erpGoodInfoService.getErpGoodInfoList(page, propertyFilterList);
	}
	
	@RequestMapping(value="/erpGoodInfo_edit.html")
	public String erpGoodInfoEditPage(HttpServletRequest request, Model model, String id){
		if(StringUtils.isNotBlank(id)){
			model.addAttribute("erpGoodInfo", erpGoodInfoService.getErpGoodInfo("erpGoodInfoId", id));
		}
		
		model.addAttribute("unitList", sysStateService.getSysStateList("erp_good_info", "good_unit"));
		
		return "page/admin/erpGoodInfo/erpGoodInfo_edit";
	}
	
	@RequestMapping(value="/erpGoodInfo_detail_{id}.html")
	public String erpGoodInfoDetailPage(Model model, @PathVariable String id){
		model.addAttribute("erpGoodInfo", erpGoodInfoService.getErpGoodInfo("erpGoodInfoId", id));
		return "page/admin/erpGoodInfo/erpGoodInfo_detail";
	}
	
	@RequestMapping(value="/erpGoodInfo_edit.do")
	@ResponseBody
	public ReturnBody erpGoodInfoEditDo(@Valid @ModelAttribute("erpGoodInfo") ErpGoodInfo erpGoodInfo, BindingResult result){
		if(result.hasErrors()){
			return new ReturnBody(false, result.getAllErrors().get(0).getDefaultMessage());
		}
		return erpGoodInfoService.saveErpGoodInfo(erpGoodInfo);
	}
	
	@RequestMapping(value="/erpGoodInfo_del_{id}.do")
	@ResponseBody
	public ReturnBody erpGoodInfoDelDo(@PathVariable String id){
		return erpGoodInfoService.delErpGoodInfo(new ErpGoodInfo(id));
	}
	
	@RequestMapping(value="/companyAllGoods.do")
	@ResponseBody
	public List<ErpGoodInfo> companyAllGoods(){
		List<PropertyFilter>  propertyFilterList = new ArrayList<PropertyFilter>();
		propertyFilterList.add(new PropertyFilter("EQB_isDel", "false"));
		propertyFilterList.add(new PropertyFilter("EQS_sysCompanyInfo.sysCompanyInfoId", getSessionSysCompanyInfo().getSysCompanyInfoId()));
		return erpGoodInfoService.getErpGoodInfoList(propertyFilterList);
	}
	
}
